package cn.elead.it.template.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import cn.elead.it.common.controller.SuperController;
import cn.elead.it.common.responses.ApiResponses;
import cn.elead.it.template.emuns.DemoStatusEnum;
import cn.elead.it.template.model.vo.Demo;
import cn.elead.it.template.service.IDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * demo信息
 * </p>
 *
 * @author luopeng
 */
@Api(tags = { "Demo" }, description = "demo信息相关接口")
@RestController
@RequestMapping(value = "/demos", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DemoController extends SuperController {

	@Autowired
	private IDemoService demoService;

	@ApiOperation("查询demo信息")
	@GetMapping
	@ApiImplicitParams({ //
			@ApiImplicitParam(name = "name", value = "名称", paramType = "query"), //
			@ApiImplicitParam(name = "status", value = "在职/离职 ", paramType = "query") }) //
	public ApiResponses<List<Demo>> findDemos( //
			@RequestParam(value = "name", required = false) String name, //
			@RequestParam(value = "status", required = true) DemoStatusEnum status) { //

		return findList(name, status);
	}

	@ApiOperation("查询demo信息集合")
	@GetMapping("/list")
	@ApiImplicitParams({ //
			@ApiImplicitParam(name = "name", value = "名称", paramType = "query"), //
			@ApiImplicitParam(name = "status", value = "在职/离职 ", paramType = "query") }) //
	public ApiResponses<List<Demo>> findList( //
			@RequestParam(value = "name", required = false) String name, //
			@RequestParam(value = "status", required = true) DemoStatusEnum status) { //

		return success(demoService.query() //
				.like(StringUtils.isNotEmpty(name), Demo::getName, name) //
				.eq(ObjectUtils.isNotEmpty(status), Demo::getStatus, status)//
				.list());
	}

	@ApiOperation("分页查询demo信息")
	@GetMapping("/list/page")
	@ApiImplicitParams({ //
			@ApiImplicitParam(name = "name", value = "名称", paramType = "query"), //
			@ApiImplicitParam(name = "status", value = "在职/离职 ", paramType = "query") }) //
	public ApiResponses<IPage<Demo>> findPageList( //
			@RequestParam(value = "name", required = false) String name, //
			@RequestParam(value = "status", required = true) DemoStatusEnum status) { //

		return success(demoService.query() //
				.like(StringUtils.isNotEmpty(name), Demo::getName, name) //
				.eq(ObjectUtils.isNotEmpty(status), Demo::getStatus, status)//
				.page(this.getPage()));
	}

	@ApiOperation(value = "查询demo详情")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "demoID", required = true, paramType = "path") })
	@GetMapping("/{id}")
	public ApiResponses<Demo> findById(@PathVariable("id") String id) {
		return success(demoService.getById(id));
	}

	@ApiOperation(value = "添加demo信息")
	@PostMapping
	public ApiResponses<Void> create(@RequestBody Demo demo) {
		demo.setId(StringUtils.EMPTY);
		demoService.save(demo);
		return success(HttpStatus.CREATED);
	}

	@ApiOperation(value = "修改demo信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "demoID", required = true, paramType = "path") })
	@PutMapping("/{id}")
	public ApiResponses<Void> update(@PathVariable("id") String id, @RequestBody Demo demo) {
		demo.setId(id);
		demoService.updateById(demo);
		return success();
	}

	@ApiOperation(value = "删除demo信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "demoID", required = true, paramType = "path") })
	@DeleteMapping("/{id}")
	public ApiResponses<Void> delete(@PathVariable("id") String courceId) {
		demoService.removeById(courceId);
		return success(HttpStatus.NO_CONTENT);
	}

}
